<!--
 @description 冻结、解冻
 @fileName Add.vue
 @author zengqiongying
 @created 2023/04/18 20:46:23
-->
<template>
  <DealerChoice @confirm="onCreat" />
</template>

<script lang="ts">
  import { getCurrentInstance, computed } from 'vue'
  import { useRouter, useRoute } from 'vue-router'
  import { useStore } from 'vuex'
  import DealerChoice from '../dealerChoice/index.vue'
  import { createCustomerChange } from '../mixins/Create'
  import { queryFreezeRange, detail, saveOrSubmit } from '@/apis/dealerMarketing/FreezeAndthaw'
  export default {
    name: 'DealerFreezeAndthawAdd',
    components: { DealerChoice },
    setup() {
      const { proxy }: any = getCurrentInstance()
      const $store = useStore()
      const $router = useRouter()
      const $route = useRoute()
      const onCreat = async (businessData: any) => {
        const pageType = `${$route.query?.type || 'E'}`
        const reportType = `${$route.query?.reportType || ''}`
        const disCode = businessData.code
        let enable = false
        await queryFreezeRange(disCode).then((res) => {
          // 查询是否有可操作的账号
          const accounts = res.data || []
          if (pageType === 'F') {
            // 解冻:确认是否有已冻结的账号
            enable = accounts.some((v: any) => [1, '1'].includes(v.state))
          } else {
            // 冻结：确认是否有未冻结的账号
            enable = accounts.some((v: any) => ![1, '1'].includes(v.state))
          }
        })

        if (!enable) {
          proxy.$toast(`当前经销商无可【${pageType === 'F' ? '解冻' : '冻结'}】的账号`)
          return
        }

        createCustomerChange(businessData, pageType, async (id: string) => {
          await detail(id).then((res) => {
            if (res.data && res.data.code) {
              const changeCode = res.data.code || ''
              const d = {
                ...res.data,
                ...{
                  freezeDTO: {
                    changeCode,
                    reportType: reportType || userReportType.value
                  }
                }
              }
              saveOrSubmit(d).then((response: any) => {
                $router.replace({
                  path: '/dealerFreezeAndthaw/edit',
                  query: {
                    id
                  }
                })
              })
            }
          })
        })
      }

      // 登录用户角色列表
      const userReportType = computed(() => {
        const user = $store.getters.userInfo || {}
        const roles = (user && user.appRoleDTOList) || []
        const names = roles.map((v: any) => v.roleName)
        console.log(names)
        if (names.includes('CTM-客户经理角色')) {
          return 'A'
        }
        if (names.includes('CTM-档案科')) {
          return 'B'
        }
        return ''
      })

      return {
        onCreat,
        userReportType
      }
    }
  }
</script>
<style lang="less" scoped></style>
